package org.elephas.webapp.backend.component;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.elephas.model.Blog;
import org.elephas.service.Elephas;
import org.elephas.service.ThemeFinder;
import org.elephas.webapp.component.Theme;
import org.elephas.webapp.model.DetachableBlogModel;

/**
 * Created Apr 6, 2008
 * 
 * @author <a href="mailto:jlee@antwerkz.com">Justin Lee</a>
 */
public class ThemeFormPanel extends Panel {
	@SpringBean
	private ThemeFinder themes;

	@SpringBean
	private Elephas elephas;

	private Theme theme;

	public ThemeFormPanel(String id, DetachableBlogModel blogModel) {
		super(id);
		final Form form = new Form("form");
		add(form);
		form.add(new DropDownChoice("configuration.theme", new PropertyModel(
				this, "theme"), themes.listThemes(), new ThemeRenderer()) {
			private static final long serialVersionUID = 1L;

			@Override
			protected CharSequence getDefaultChoice(Object selected) {
				return null;
			}

			@Override
			protected void onSelectionChanged(Object newSelection) {
				Theme theme = (Theme) getModelObject();
				Blog blog = (Blog) form.getModelObject();
				blog.getConfiguration().setTheme(theme.getName());
				elephas.saveBlog(blog);
			}

			@Override
			protected boolean wantOnSelectionChangedNotifications() {
				return true;
			}
		});
	}
}
